package rents.internal.rent;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.stereotype.Repository;

import rents.AccountContribution;
import rents.RentConfirmation;
import rents.User;

import common.datetime.SimpleDate;

@Repository
public abstract class JdbcRentRepository extends SimpleJdbcDaoSupport implements RentRepository {
	
	@Autowired
	public JdbcRentRepository(DataSource dataSource){
		setDataSource(dataSource);
	}

	public RentConfirmation confirmReward(AccountContribution contribution, User user) {
		
		String sql = "insert into T_REWARD (CONFIRMATION_NUMBER, REWARD_AMOUNT, REWARD_DATE, ACCOUNT_NUMBER, DINING_MERCHANT_NUMBER, DINING_DATE, DINING_AMOUNT) values (?, ?, ?, ?, ?, ?, ?)";
		String confirmationNumber = nextConfirmationNumber();
		getSimpleJdbcTemplate().update(sql, confirmationNumber, contribution.getAmount().asBigDecimal(),
				SimpleDate.today().asDate(), contribution.getAccountNumber(), user.getMerchantNumber(),
				user.getDate().asDate(), user.getAmount().asBigDecimal());
		return new RentConfirmation(confirmationNumber, contribution);
	}

	private String nextConfirmationNumber() {
		String sql = "select next value for S_REWARD_CONFIRMATION_NUMBER from DUAL_REWARD_CONFIRMATION_NUMBER";
		return getSimpleJdbcTemplate().queryForObject(sql, String.class);
	}

}
